import router from '../../router/index';
import dynamicRouters from '../../router/dynamicRouters';
import { getUserRolePower } from '@/api/userRole.js';
import generateRouters from '../../utils/generateRouters';
import generateButtons from '../../utils/generateButtons';

const state = {
  routes: [],
  buttons: []
};

const mutations = {
  // 设置路由值
  setRoutes: (state, routes) => {
    state.routes = routes;
  },
  // 设置按钮权限值
  setButtons: (state, buttons) => {
    state.buttons = buttons;
  }
};

const actions = {
  async generateRoutes({ commit, state }) {
    // 获取用户路由权限
    // let authorityData = await getUserRolePower({
    //   request_name: 'iot_system'
    // });
    // const userRouters = authorityData.data[0];
    // // 根据权限树生成动态菜单路由
    // const dynamicRouters = await generateRouters(userRouters);
    // // 获取所有按钮权限
    // const permissionButtons = await generateButtons(userRouters);
    // return new Promise(resolve => {
    //   router.addRoutes(dynamicRouters);
    //   commit('setRoutes', dynamicRouters[0].children);
    //   commit('setButtons', permissionButtons);
    //   resolve();
    // });

    return new Promise(resolve => {
      router.addRoutes(dynamicRouters);
      commit('setRoutes', dynamicRouters);
      resolve();
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
